package com.kusej.idno.generation;

import com.kusej.idno.generation.utils.SerializationUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Global {
    public static String parentCode = "b8346a212a5611e78191082e5f152c71";
    public static RegionModel defaultProvince;
    public static RegionModel defaultCity;
    public static RegionModel defaultDistrict;
    public static ArrayList<RegionModel> regionAll = SerializationUtils.toObj(ArrayList.class);
    public static Map<String, RegionModel> regionProvince = new ConcurrentHashMap<>();
    public static Map<String, List<RegionModel>> regionCity = new ConcurrentHashMap<>();
    public static Map<String, List<RegionModel>> regionDistrict = new ConcurrentHashMap<>();
    public static Map<String, String> code2FullName = SerializationUtils.readRegionAll();
    public static Map<String, RegionModel> fullName2Model = new ConcurrentHashMap<>();

    static {
        if (regionAll != null && !regionAll.isEmpty()) {
            regionAll.forEach(a -> {
                if (a.getParentId().equals(parentCode)) {
                    // 设置省
                    regionProvince.put(a.getId(), a);
                } else if (regionProvince.containsKey(a.getParentId())) {
                    // 设置市
                    List<RegionModel> temp = regionCity.get(a.getParentId());
                    if (temp == null) {
                        temp = new ArrayList<>();
                    }
                    temp.add(a);
                    regionCity.put(a.getParentId(), temp);
                } else {
                    // 设置区
                    List<RegionModel> temp = regionDistrict.get(a.getParentId());
                    if (temp == null) {
                        temp = new ArrayList<>();
                    }
                    temp.add(a);
                    regionDistrict.put(a.getParentId(), temp);
                }
                code2FullName.put(a.getCode(), a.getFullName());
                fullName2Model.put(a.getFullName(), a);
            });
        }
        defaultProvince = new RegionModel();
        defaultProvince.setName("请选择省份");
        fullName2Model.put("请选择省份",defaultProvince);
        defaultCity = new RegionModel();
        defaultCity.setName("请选择城市");
        fullName2Model.put("请选择城市",defaultCity);
        defaultDistrict = new RegionModel();
        defaultDistrict.setName("请选择区县");
        fullName2Model.put("请选择区县",defaultDistrict);
    }

}
